const goods = require("./goodsModule");


function addgoods(goodsname,price,des,thumb,sellerID){
    return new Promise((resolve,reject)=>{
        new goods({
            goodsname,price,des,thumb,sellerID
        }).save().then(
            (result)=>{
                if(result){
                    resolve("保存成功")
                }else{
                    reject("保存失败")
                }
            }
        )
    })
}

function findgoods(id){
    return new Promise((resolve,reject)=>{
        goods.find({sellerID:id})
        .then(
            (result)=>{
                resolve(result);
            },
            ()=>{
                reject("数据库错误");
            }
        )
    })
}


function deletegoods(id){
    return new Promise((resolve,reject)=>{
        goods.findByIdAndRemove(id)
        .then(
            (result)=>{
                if(result){
                    resolve();
                }else{
                    reject();
                }
            },
            ()=>{
                reject();
            }
        )
    })
}


function findchangegood(id){
    return new Promise((resolve,reject)=>{
        goods.findById(id)
        .then(
            (result)=>{
                if(result){
                    resolve(result);
                }else{
                    reject();
                }
            },
            ()=>{
                reject("数据库错误");
            }
        )
    })
}


function changegood(goodsid,goodsname,price,des){
    return new Promise((resolve,reject)=>{
        goods.findByIdAndUpdate(
            //查找id
            goodsid,
            //修改的值
            {goodsname,price,des}
        ).then(
            (result)=>{
                if(result){
                    resolve();
                }else{
                    reject();
                }
            },
            ()=>{
                reject("数据库错误");
            }
        )
    })
}


function findgoodslist(page,count){
    return new Promise((resolve,reject)=>{
        goods.find().count().then(
            (result)=>{
                let pages = Math.ceil(result/count);
                let skipVal = (page-1)*count;
                goods.find().skip(skipVal).limit(count)
                .then(
                    (result)=>{
                        resolve({result,pages})
                    }
                )
            },
            ()=>{
                reject("数据库错误")
            }
        )
    })
}

function searchgoodsbyname(page,count,search){
    let reg = new RegExp(search,"i")
    return new Promise((resolve,reject)=>{
        goods.find({goodsname:{$regex:reg}}).count().then(
            (result)=>{
                let pages = Math.ceil(result/count);
                let skipVal = (page-1)*count;
                goods.find({goodsname:{$regex:reg}}).skip(skipVal).limit(count)
                .then(
                    (result)=>{
                        resolve({result,pages})
                    }
                )
            },
            ()=>{
                reject("数据库错误")
            }
        )
    })
}

module.exports = {addgoods,findgoods,deletegoods,findchangegood,changegood,findgoodslist,searchgoodsbyname}